<?
  if ($val) 
  {
    $file = db_read_rec( sprintf("SELECT name, type, file, length(file) FROM files WHERE id=%d", $val) );

    if ($file)
    {
      $type = $file["type"];
      if (!$type)
        { $type = "application/octet-stream"; };

      header("Content-length:{$file["length(file)"]}");
      header("Content-type:{$type}");
      header("Content-Disposition: attachment; filename={$file["name"]}");

      echo $file["file"];

      db_register_file_download($val);
    }
  }
?>
